# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from lxml import etree
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import datetime
from pymongo import MongoClient

# 初始化
def init_driver(url):
    chrome_options = Options()
    # chrome_options.add_argument('--headless') # 无头模式
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36')  # 修改请求头，伪装浏览器型号
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) # 设置开发模式，避免被检测
    # driver = webdriver.Chrome(chrome_options=chrome_options) # 已经设置PATH环境变量
    # 未在PATH环境变量中设置，需要设置驱动可执行路径
    driver = webdriver.Chrome(executable_path='D:\\webdriver\\chromedriver.exe', chrome_options=chrome_options)
    driver.maximize_window()  # 窗口最大化
    driver.get(url)
    # html = driver.page_source
    # print(html)
    return driver


# 获取页面url
def get_url(drive):
    # 设置等待
    wait = WebDriverWait(driver, 10)

    while True:
        try:
            html = etree.HTML(driver.page_source)
            for x in html.xpath('//div[@class="txt-box"]'):
                dict_data = dict()
                dict_data['title'] = "".join(x.xpath('./h3/a//text()'))
                dict_data['titleUrl'] = x.xpath('./h3/a/@href')[0]
                dict_data['titleContent'] = "".join(x.xpath('./p[@class="txt-info"]//text()'))
                dict_data['titleFqr'] = x.xpath('./div[@class="s-p"]/a/text()')[0]
                push_time = x.xpath('./div[@class="s-p"]/span/text()')[0]
                if push_time.find("天前") > 0:
                    push_time = "-" + push_time.replace("天前", "")
                    push_time = (datetime.datetime.now() + datetime.timedelta(days=int(push_time))).strftime("%Y-%m-%d")
                elif push_time.find("昨天") > 0:
                    push_time = push_time.replace("昨天", "")
                    push_time = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime("%Y-%m-%d")
                elif push_time.find("小时前") > 0:
                    push_time = (datetime.datetime.now()).strftime("%Y-%m-%d")
                dict_data['titleTime'] = push_time
                url_child = dict_data['titleUrl']
                print(f"=============标题：{dict_data['title']}=================")
                if url_child.find("http") < 0:
                    url_child = "https://weixin.sogou.com" + url_child
                js = f'window.open("{url_child}");'
                driver.execute_script(js)
                print(f"=============打开页面=================")
                # 获取当前窗口句柄
                now_handle = driver.current_window_handle
                # driver.find_element_by_xpath('//div[@class="txt-box"]/h3/a').click()

                # 获取所有窗口句柄
                all_handles = driver.window_handles
                for handle in all_handles:
                    if handle != now_handle:
                        driver.switch_to.window(handle)
                        time.sleep(2)
                        try:
                            print(f"=============获取信息中...=================")
                            WebDriverWait(driver, 10).until(
                                EC.presence_of_element_located((By.CLASS_NAME, "rich_media_title")))
                        except:
                            driver.close()
                            break
                        root_child = etree.HTML(driver.page_source)
                        dict_data['content'] = "".join(root_child.xpath('//div[@id="js_content"]//text()')).replace("\r", "").replace("\n", "").strip()
                        driver.close()
                driver.switch_to.window(now_handle)

                insertItem("搜狗微信", dict_data)
            page = html.xpath('.//div[@class="p-fy"]/span')[0].text
            print(f"===========第【{page}】页===========")
            time.sleep(2)
            try:
                element = WebDriverWait(driver, 10).until(
                    EC.presence_of_element_located((By.ID, "sogou_next"))).click()
            except:
                pass
        except:
            pass



def close_driver(driver):
    driver.quit()

def insertItem(tableName, data):
    my_set = db[tableName]
    my_set.insert_one(data)

if __name__ == '__main__':
    conn = MongoClient('127.0.0.1', 27017)
    db = conn["Test"]
    url = "https://weixin.sogou.com/"
    driver = init_driver(url)
    get_url(driver)
